package com.asiainfo.controller;

import com.alibaba.fastjson.JSON;
import com.asiainfo.bean.ResponseBodyVo;
import com.asiainfo.bean.ResponseVo;
import com.asiainfo.conf.Constants;
import com.asiainfo.rsa.IOUtil;
import com.asiainfo.rsa.RSASignature;
import com.asiainfo.services.AuthService;
import com.asiainfo.utils.ConfigurationUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by ThinkPad on 2018/1/3.
 */
@Controller
public class AuthController {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static Logger log = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    AuthService authService;

    @RequestMapping("/index")
    @ResponseBody
    public String index(HttpServletRequest request) {
        String recordNo = request.getParameter("recordNo");
        ResponseBodyVo body = new ResponseBodyVo();
        body.setRecordNo(recordNo);
        body.setEncryptContent("EncryptContent");
        body.setAuthorizeTime(sdf.format(new Date()));
        body.setIsVaild(true);
        body.setAuzCertificate("授权凭证");
        body.setEncryption("加密方式");
        body.setContentVer("密文标识");
        body.setAuthorizeOrgNo("授权机构编号");
        body.setAuthenticateOrgNo("鉴权机构ID");
        body.setAuthenticateTime("鉴权时间");
        ResponseVo responseVo = new ResponseVo();
        responseVo.setBody(body);
        responseVo.setErrMsg("操作成功");
        responseVo.setErrorCode(0);
        responseVo.setMessage("");
        String content = authService.responseBodyVoToStr(body);
        String privateKey = IOUtil.loadKeyByFile(ConfigurationUtil.getPrivateKeyPath());
        String signature = RSASignature.sign(content, privateKey);
        responseVo.setSignature(signature);
        return JSON.toJSONString(responseVo);
    }

    /**
     * 授权查验
     */
    @RequestMapping("/authorizeCheck")
    @ResponseBody
    public String authorizeCheck(String authorizeNo) {
        String authResult = authService.authCheck(Constants.AUTHORIZE_NO, authorizeNo,
            ConfigurationUtil.getAuthorizeCheckURL());
        boolean checkResult = authService.checkAuthResult(authResult);
        return String.valueOf(checkResult);
    }

    /**
     * 鉴权查验
     */
    @RequestMapping("/authenticationCheck")
    @ResponseBody
    public String authenticationCheck(String authenticationNo) {
        String authResult = authService.authCheck(Constants.AUTHENTICATION_NO, authenticationNo,
            ConfigurationUtil.getAuthenticationCheckURL());
        boolean checkResult = authService.checkAuthResult(authResult);
        return String.valueOf(checkResult);
    }


}
